package com.wilmar.base.dao;

import java.util.List;

public class PaginationSupport {

	public static final int PAGESIZE = 15;

	private List items;
	private long totalCount;
	private int pageSize = PAGESIZE;
	private int[] indexes = new int[0];
	private int pageCount;
	private int startIndex;

	public int[] getIndexes() {
		return indexes;
	}

	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	/**
	 * @return the currentSize
	 */
	public int getCurrentSize() {
		if ((this.totalCount - (long) this.startIndex) >= (long) pageSize) {
			return this.pageSize;
		} else {
			return (int) (this.totalCount % (long) pageSize);
		}
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				count++;
			pageCount = count;
			indexes = new int[count];
			for (int i = 0; i < count; i++) {
				indexes[i] = pageSize * i;
			}
		} else {
			this.totalCount = 0;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		if (totalCount <= 0)
			this.startIndex = 0;
		else if (startIndex >= totalCount)
			this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0)
			this.startIndex = 0;
		else {
			this.startIndex = indexes[startIndex / pageSize];
		}
	}

	public PaginationSupport(List items, int totalCount) {
		this.items = items;
		this.totalCount = totalCount;
	}

	public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
		this.items = items;
		this.setTotalCount(totalCount);
		this.pageSize = pageSize;
		this.startIndex = startIndex;
	}

	public static int convertFromPageToStartIndex(int pageNo) {
		return (pageNo - 1) * PAGESIZE;
	}

	public static int convertFromPageToStartIndex(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}

	public int getNextIndex() {
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= totalCount)
			return getStartIndex();
		else
			return nextIndex;
	}

	/**
	 * 获得上页起始位置
	 */
	public int getPreviousIndex() {
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0)
			return 0;
		else
			return previousIndex;
	}

	/**
	 * 获得最后一页起始位置
	 */
	public long getLastIndex() {
		if (indexes.length < 1) {
			return 0;
		}
		return indexes[indexes.length - 1];
	}

	/**
	 * 取总页数.
	 */
	public long getTotalPageCount() {
		if (totalCount % pageSize == 0)
			return totalCount / pageSize;
		else
			return totalCount / pageSize + 1;
	}

	/**
	 * 取该页当前页码,页码从1开始.
	 */
	public long getCurrentPageNo() {
		return startIndex / pageSize + 1;
	}

	/**
	 * 该页是否有下一页.
	 */
	public boolean isHasNextPage() {
		return this.getCurrentPageNo() < this.getTotalPageCount();
	}

	/**
	 * 该页是否有上一页.
	 */
	public boolean isHasPreviousPage() {
		return this.getCurrentPageNo() > 1;
	}

	/**
	 * 是否是首页
	 * 
	 * @return
	 */
	public boolean isFirstPage() {
		return this.getCurrentPageNo() == 1;
	}

	/**
	 * 是否最后一页
	 */
	public boolean isLastPage() {
		return this.getCurrentPageNo() == this.getTotalPageCount();
	}

	/**
	 * @return the pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * @param pageCount the pageCount to set
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
}
